﻿using System.Threading.Tasks;
using XCI.Sys.Model;
using XCI.Sys.Service;
using XCI.Windows;
using XCI.Helper;

namespace XCI.Sys.Controls
{
    public partial class DeptOwnerTreeList : BaseUserControl
    {
        public DeptOwnerTreeList()
        {
            InitializeComponent();
            treeDept.ModelType = typeof(SysDept);
        }

        private void BindCore(SysRolePermission sysRolePermission)
        {
            if (sysRolePermission.DeptScope == 2)//自定义
            {
                checkCustom.Checked = true;
                var data = sysRolePermission.CustomDepts;
                treeDept.DataSource = data;
                treeDept.ExpandAll();
            }
            else
            {
                WinFormHelper.SetRadioGroupCheckedValue(sysRolePermission.DeptScope, checkAll, checkDept,
                checkDeptAndLower, checkSelf, checkCustom);
            }
        }

        public async Task LoadByRoleAsync(string roleId)
        {
            BindCore(await SysRoleService.Instance.SelectRolePermissionAsync(roleId));
        }

        public async Task LoadByUserAsync(string userId)
        {
            BindCore(await SysAccountService.Instance.SelectUserPermissionAsync(userId));
        }
    }
}
